home *** CD-ROM | disk | FTP | other *** search
/ Inter.Net 55-1 / Inter.Net 55-1.iso / CBuilder / Setup / BCB / data.z / stdmutex.h < prev    next >
Encoding:
C/C++ Source or Header  |  1998-02-09  |  7.3 KB  |  255 lines

  1. #ifndef __RWSTD_MUTEX_H__
  2. #define __RWSTD_MUTEX_H__
  3. #pragma option push -b -a4 -Vx- -Ve- -w-inl -w-aus -w-sig
  4.  
  5. /*
  6.  * Declarations for class _RWSTDMutex and _RWSTDGuard.
  7.  *
  8.  * $Id: stdmutex.h,v 1.14 1996/08/28 01:30:38 smithey Exp $
  9.  *
  10.  ***************************************************************************
  11.  *
  12.  * (c) Copyright 1994, 1995 Rogue Wave Software, Inc.
  13.  * ALL RIGHTS RESERVED *
  14.  * The software and information contained herein are proprietary to, and
  15.  * comprise valuable trade secrets of, Rogue Wave Software, Inc., which
  16.  * intends to preserve as trade secrets such software and information.
  17.  * This software is furnished pursuant to a written license agreement and
  18.  * may be used, copied, transmitted, and stored only in accordance with
  19.  * the terms of such license and with the inclusion of the above copyright
  20.  * notice.  This software and information or any other copies thereof may
  21.  * not be provided or otherwise made available to any other person.
  22.  *
  23.  * Notwithstanding any other lease or license that may pertain to, or
  24.  * accompany the delivery of, this computer software and information, the
  25.  * rights of the Government regarding its use, reproduction and disclosure
  26.  * are as set forth in Section 52.227-19 of the FARS Computer
  27.  * Software-Restricted Rights clause.
  28.  * 
  29.  * Use, duplication, or disclosure by the Government is subject to
  30.  * restrictions as set forth in subparagraph (c)(1)(ii) of the Rights in
  31.  * Technical Data and Computer Software clause at DFARS 252.227-7013.
  32.  * Contractor/Manufacturer is Rogue Wave Software, Inc.,
  33.  * P.O. Box 2328, Corvallis, Oregon 97339.
  34.  *
  35.  * This computer software and information is distributed with "restricted
  36.  * rights."  Use, duplication or disclosure is subject to restrictions as
  37.  * set forth in NASA FAR SUP 18-52.227-79 (April 1985) "Commercial
  38.  * Computer Software-Restricted Rights (April 1985)."  If the Clause at
  39.  * 18-52.227-74 "Rights in Data General" is specified in the contract,
  40.  * then the "Alternate III" clause applies.
  41.  *
  42.  ***************************************************************************
  43.  *
  44.  * This class is a portable implementation of a simple mutex lock
  45.  * to be used for synchronizing multiple threads within a single process.
  46.  * It is not suitable for use among threads of different processes.
  47.  * This code was taken from the tools mutex.h.
  48.  * 
  49.  ***************************************************************************/
  50.  
  51. #include <stdcomp.h>
  52.  
  53. #ifndef __RWSTDDEFS_H__
  54. #include "rw/stddefs.h"
  55. #endif
  56.  
  57. #ifdef _RWSTD_MULTI_THREAD /* This class only relevant in MT situation */
  58.  
  59. #if defined(sun)          /* assuming Solaris 2.1 or greater */
  60. #include <synch.h>
  61. typedef mutex_t _RWSTDMutexType;
  62. #elif defined(_RWSTD_POSIX_THREADS)
  63. #include <pthread.h>
  64. typedef pthread_mutex_t _RWSTDMutexType;
  65. #define _RWSTD_NEEDS_SEM_INIT
  66. #elif defined(__WIN32__)
  67. #include <windows.h>
  68. typedef CRITICAL_SECTION _RWSTDMutexType;
  69. #define _RWSTD_NEEDS_SEM_INIT
  70. #elif defined(__OS2__)
  71. #include <exception>
  72. #define INCL_DOSSEMAPHORES
  73. #define _RWSTD_NEEDS_SEM_INIT
  74. #include <os2.h>
  75. typedef HMTX _RWSTDMutexType;
  76. extern const char* __rw_mutex_exception;
  77.  
  78. class _RWSTDExport thread_error : public exception
  79. {
  80.   public:
  81.     thread_error () _RWSTD_THROW_SPEC_NULL : exception( )
  82.     { ; }
  83.     virtual const char * what () const  _RWSTD_THROW_SPEC_NULL
  84.     {
  85.         return __rw_mutex_exception;
  86.     }
  87. };                            
  88.  
  89. #else
  90. #error Class _RWSTDMutex is not yet supported in this environment
  91. #endif
  92.  
  93. class _RWSTDMutex
  94. {
  95.   private:
  96.  
  97.     _RWSTDMutexType mutex;
  98. #if defined(_RWSTD_NEEDS_SEM_INIT)
  99.     int initFlag;
  100. #endif
  101.  
  102.     void init ();
  103.     //
  104.     // Disallow assignment.
  105.     //
  106.     _RWSTDMutex& operator= (const _RWSTDMutex&);
  107.  
  108. public:
  109.  
  110.   enum StaticCtor { staticCtor };
  111.  
  112.   _RWSTDMutex ();             // Construct the mutex.
  113.   _RWSTDMutex (const _RWSTDMutex&); // Construct the mutex
  114.   _RWSTDMutex (StaticCtor);   // Some statics need special handling.
  115.   ~_RWSTDMutex ();            // Destroy the mutex.
  116.  
  117.   void acquire ();           // Acquire the mutex.
  118.   void release ();           // Release the mutex.
  119. };
  120.  
  121. class _RWSTDGuard
  122. {
  123.   private:
  124.  
  125.     _RWSTDMutex& rwmutex;
  126.  
  127.   public:
  128.  
  129.     _RWSTDGuard  (_RWSTDMutex& m);  // Acquire the mutex.
  130.     ~_RWSTDGuard ();               // Release the mutex.
  131. };
  132.  
  133. /*
  134. ** For those OSs that require a non-zero mutex, we must treat static 
  135. ** mutexes specially; they may not be initialized when we need them.
  136. ** For efficiency, we do conditional compilation in several methods
  137. ** based on that need.
  138. */
  139.  
  140. inline _RWSTDMutex::_RWSTDMutex (_RWSTDMutex::StaticCtor)
  141. {
  142.     //
  143.     // Empty, because acquire() may already have been used.
  144.     //
  145. }
  146.  
  147. inline _RWSTDMutex::~_RWSTDMutex () 
  148. #if defined(_RWSTD_NEEDS_SEM_INIT)
  149.     if (0 == initFlag)
  150.         return;
  151.     else
  152.         initFlag = 0;
  153. #endif
  154. #if defined(sun)
  155.     mutex_destroy(&mutex); 
  156. #elif defined(_RWSTD_POSIX_THREADS)
  157.     pthread_mutex_destroy(&mutex); 
  158. #elif defined(__WIN32__)
  159.     DeleteCriticalSection(&mutex);
  160. #elif defined(__OS2__)
  161.     APIRET rv;
  162.     _RWSTD_THROW_NO_MSG(0 != (rv = DosCloseMutexSem(mutex)),
  163.                 thread_error);
  164. #endif
  165. }
  166.  
  167.  
  168. void inline _RWSTDMutex::init ()
  169. #if defined(sun)
  170.     mutex_init(&mutex, USYNC_THREAD, NULL); 
  171. #elif defined(_RWSTD_POSIX_THREADS)
  172. # ifdef SNI_SINIX
  173.     pthread_mutex_init(&mutex, pthread_attr_default);
  174. # elif defined(_RWSTD_POSIX_D10_THREADS)
  175. #   if defined(_RWSTD_MUTEXATTR_DEFAULT)
  176.     pthread_mutex_init(&mutex, &pthread_mutexattr_default);
  177. #    else
  178.     pthread_mutex_init(&mutex, 0);
  179. #    endif
  180. # else
  181.     pthread_mutex_init(&mutex, pthread_mutexattr_default);
  182. # endif
  183. #elif defined(__WIN32__)
  184.     InitializeCriticalSection(&mutex);
  185. #elif defined(__OS2__)
  186.     APIRET rv;
  187.     _RWSTD_THROW_NO_MSG(0 != (rv = DosCreateMutexSem(0,&mutex,DC_SEM_SHARED,FALSE)),
  188.                 thread_error);
  189. #endif
  190. #if defined(_RWSTD_NEEDS_SEM_INIT)
  191.     initFlag = 1;
  192. #endif
  193. }  
  194.  
  195. inline _RWSTDMutex::_RWSTDMutex () 
  196.   init(); 
  197. }  // Initialize the mutex.   
  198.  
  199. inline _RWSTDMutex::_RWSTDMutex (const _RWSTDMutex&)
  200.   init(); 
  201. }  // Initialize the mutex.   
  202.  
  203. inline void _RWSTDMutex::acquire ()
  204. #if defined(_RWSTD_NEEDS_SEM_INIT)
  205.     if(0 == initFlag)
  206.         init();
  207. #endif
  208. #if defined(sun)
  209.     mutex_lock(&mutex);    
  210. #elif defined(_RWSTD_POSIX_THREADS)
  211.     pthread_mutex_lock(&mutex);    
  212. #elif defined(__WIN32__)
  213.     EnterCriticalSection(&mutex);
  214. #elif defined(__OS2__)
  215.     APIRET rv;
  216.     _RWSTD_THROW_NO_MSG(0 != (rv = DosRequestMutexSem(mutex, SEM_INDEFINITE_WAIT)),
  217.                thread_error); 
  218. #endif
  219. }
  220.  
  221. inline void _RWSTDMutex::release ()
  222. #if defined(sun)
  223.     mutex_unlock(&mutex);  
  224. #elif defined(_RWSTD_POSIX_THREADS)
  225.     pthread_mutex_unlock(&mutex);  
  226. #elif defined(__WIN32__)
  227.     LeaveCriticalSection(&mutex);
  228. #elif defined(__OS2__)
  229.     APIRET rv;
  230.     _RWSTD_THROW_NO_MSG(0 != (rv = DosReleaseMutexSem(mutex)),
  231.                 thread_error);
  232. #endif
  233. }
  234.  
  235. inline _RWSTDGuard::_RWSTDGuard (_RWSTDMutex& m) : rwmutex(m)
  236. {
  237.     rwmutex.acquire();
  238. }
  239.  
  240. inline _RWSTDGuard::~_RWSTDGuard () { rwmutex.release(); }
  241.  
  242.  
  243. #define STDGUARD(a) _RWSTDGuard(a)
  244. #else
  245. #define STDGUARD(a) 
  246. #endif  /*_RWSTD_MULTI_THREAD*/
  247. #pragma option pop
  248. #endif  /*__RWSTD_MUTEX_H__*/
  249.